package com.jiantong.admin.controller.dongjiantong;

import com.jiantong.admin.controller.DjtBaseController;
import com.jiantong.admin.model.PagingMz;
import com.jiantong.lawEnforcing.model.BorrowRecord;
import com.jiantong.lawEnforcing.service.*;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 执法办案
 * Created by ThinkPad on 2017/8/16.
 */

@Slf4j
@Controller
@RequestMapping("/djt/pollcelow")
public class pollceloeController extends DjtBaseController {

    //问答记录
    @RpcConsumer
    private AskAnswerService askAnswerService;

    //借阅记录
    @RpcConsumer
    private BorrowRecordService borrowRecordService;

    //案件基础信息
    @RpcConsumer
    private CasesBasicService casesBasicService;

    //立案审批
    @RpcConsumer
    private CasesCheckService casesCheckService;

    //案由
    @RpcConsumer
    private CauseActionsService causeActionsService;

    //物证信息
    @RpcConsumer
    private EvidencesService evidencesService;

    //法律条文
    @RpcConsumer
    private LegalProvisionsService legalProvisionsService;

    //当事人信息
    @RpcConsumer
    private PartiesInformationService partiesInformationService;

    //当场处罚决定
    @RpcConsumer
    private SimplenessCasesService simplenessCasesService;

    //文书存储表
    @RpcConsumer
    private WritDetailsService writDetailsService;

    //文书其它信息
    @RpcConsumer
    private WritRelevantService writRelevantService;


    private static final String BaseViews = VIEWS +"pollcelow/";

    /**
     * 执法办案首页
     * @return
     */
    @RequestMapping("/wait_home")
    private String index(){

        return BaseViews+"/wait_home";
    }

    /**
     *待审核案件
     * @return
     */
    @RequestMapping("/wait_audit")
    private String wait_audit(){

        return BaseViews+"/wait_audit";
    }

    /**
     *最新归档案件
     * @return
     */
    @RequestMapping("/nearest_archived_file")
    private String nearest_archived_file(){

        return BaseViews+"/nearest_archived_file";
    }

    /**
     *案件登记
     * @return
     */
    @RequestMapping("/register")
    private String register(){

        return BaseViews+"/register";
    }

    /**
     *当场处罚
     * @return
     */
    @RequestMapping("/immediate_punish")
    private String immediate_punish(){

        return BaseViews+"/immediate_punish";
    }

    /**
     *立案审批
     * @return
     */
    @RequestMapping("/criticize")
    private String criticize(){

        return BaseViews+"/criticize";
    }

    /**
     * 文书制作
     * @return
     */
    @RequestMapping("/writ")
    private String writ(){

        return BaseViews+"/writ";
    }

    /**
     * 案件审核
     * @return
     */
    @RequestMapping("/audit")
    private String audit(){

        return BaseViews+"/audit";
    }

    /**
     * 档案借阅
     * @return
     */
    @RequestMapping("/borrow")
    private String borrow(){

        return BaseViews+"/borrow";
    }

    /**
     * 授权审核
     * @return
     */
    @RequestMapping("/borrow_audit")
    private String borrow_audit(){

        return BaseViews+"/borrow_audit";
    }

    /**
     * 申请记录
     * @return
     */
    @RequestMapping("/apply_for")
    private String apply_for(){

        return BaseViews+"/apply_for";
    }

    /**
     * 借阅记录页面信息提供接口
     * @param page
     * @param rows
     * @param breedFactoryId
     * @param isScale
     * @return
     */
    @GetMapping("/borrowRecord")
    @ResponseBody
    public PagingMz<BorrowRecord> showBreedFactory(@RequestParam(required = false) Integer page,
                                                    @RequestParam(required = false) Integer rows,
                                                    @RequestParam(required = false) String breedFactoryId,
                                                    @RequestParam(required = false) Integer isScale) {
        Map criteria = new HashMap();
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 20;
        }
        if (breedFactoryId == null) {
            criteria.put("breedFactoryIds", null);
        }

        criteria.put("isScale", isScale);

        Response<Paging> response = borrowRecordService.paging(page, rows, criteria);
        Paging paging = response.getResult();
        Long records = paging.getTotal();
        Long pagetotal = (records - 1) / rows + 1;
        return new PagingMz<>(pagetotal, records, page, paging.getData());
    }


    //management 接口  /djt/pollcelow

    /**
     * 借阅记录详细信息
     * @return
     */
    @GetMapping("/select")
    public String select() {
        return "management/law/borrowPop";//页面地址
    }

    /**
     * 借阅记录更新
     * @return
     */
    @GetMapping("/update")
    public String update() {
        return "management/law/borrowUpdate";//页面地址
    }

    /**
     *
     * @param id

     * @param remark
     * @param borrow_date
     * @param borrow_end_date
     * @return
     */
    @RequestMapping("/update1")
    @ResponseBody
    public Boolean update(@RequestParam(required = false) Long id,
                          @RequestParam(required = false) Integer status,
                          @RequestParam(required = false) String remark,
                          @RequestParam(required = false) String borrow_date,
                          @RequestParam(required = false) String borrow_end_date) {
        BorrowRecord borrowRecord = new BorrowRecord();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date start=null,end=null;
        try {
            if (borrow_date!=null) {
                start = sdf.parse(borrow_date);
            }
            if (borrow_end_date!=null) {
                end = sdf.parse(borrow_end_date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Date start = new Date(borrow_date);
//        Date end = new Date(borrow_end_date);
        borrowRecord.setId(id);
        borrowRecord.setStatus(status);
        borrowRecord.setRemark(remark);
        borrowRecord.setBorrow_date(start);
        borrowRecord.setBorrow_end_date(end);
        Response<Boolean> response = borrowRecordService.update(borrowRecord);
        return response.getResult();
    }

    /*

    @GetMapping("/borrow")
    @ResponseBody
    public PagingMz<BorrowRecord> showBreedFactory(@RequestParam(required = false) Integer page,
                                                   @RequestParam(required = false) Integer rows,
                                                   @RequestParam(required = false) Long breedFactoryId,
                                                   @RequestParam(required = false) Integer isScale) {
        Map criteria = new HashMap();
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 20;
        }

        criteria.put("isScale", isScale);

        Response<Paging> pagingResponse = borrowRecordService.paging(page, rows, criteria);
        List<BorrowRecord> resultList = new ArrayList<>();
        Long records = pagingResponse.getResult().getTotal();
        Long pagetotal = (records - 1) / rows + 1;

        return new PagingMz<>(pagetotal, records, page, resultList);
    }
*/

}
